
//
//  WarDetailViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/18.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "WarDetailViewC.h"
#import "UserModel.h"
#import "WarEntity.h"
#import "Util.h"
#import <UIImageView+AFNetworking.h>
#import "UserModel.h"
#import "MoreWarMViewC.h"
#import <ShareSDK/ShareSDK.h>
#import <MBProgressHUD.h>

@interface WarDetaiCCell : UICollectionViewCell
@property(nonatomic, weak) IBOutlet UIImageView *wheadImg;
@property(nonatomic, weak) IBOutlet UILabel *nameLab;
@end
@implementation WarDetaiCCell
- (void)viewWillAppear:(BOOL)animated{
    [self viewWillAppear:YES];
    self.wheadImg.layer.cornerRadius = 25;
    self.wheadImg.clipsToBounds = YES;
}
@end


@interface WarDetailViewC (){
    NSArray *dataArray;
    NSArray *cellArray;
    NSMutableArray *allArray;
}

@end

@implementation WarDetailViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"约战详情";
    _headImg.layer.cornerRadius = 30;
    _headImg.clipsToBounds = YES;
    allArray = [[NSMutableArray alloc] init];
    
    if ([UserModel sharedInstance].currentUser == nil) {
        [Util presentLoginControllerFromViewController:self];
    }
    [self getData];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    UIBarButtonItem *rightBtn1 = [[UIBarButtonItem alloc]
                                  initWithImage:[UIImage imageNamed:@"shareimage"]
                                  style:UIBarButtonItemStylePlain
                                  target:self action:@selector(shareBtn:)];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)shareBtn:(id)sender{
    
    NSString* path = [[NSBundle mainBundle]pathForResource:@"ShareSDK" ofType:@"png"];
    id<ISSContent> publishContent = [ShareSDK content:@"你好,请填写分享内容!" defaultContent:nil image:[ShareSDK imageWithPath:path] title:@"添加标题" url:@"http://mob.com" description:@"添加描述" mediaType:SSPublishContentMediaTypeNews];
    //2.调用分享菜单分享
    [ShareSDK showShareActionSheet:nil shareList:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        //如果分享成功
        if (state == SSResponseStateSuccess) {
            //            NSLog(@"分享成功");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
        //如果分享失败
        if (state == SSResponseStateFail) {
            //            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
        if (state == SSResponseStateCancel){
            //            NSLog(@"分享取消");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享取消" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }];
}

- (void)getData{
    NSString *idNum = [UserModel sharedInstance].currentUser.ID;
    NSString *war = self.warID;
    [[UserModel sharedInstance] getWarDetailWithWarid:war userid:idNum getData:^(id info) {
        NSDictionary *dic = [info copy];
        dataArray = [NSArray arrayWithObject:dic];
        cellArray = info[@"accounts"];
        [_collectionView reloadData];
        [self tableviewAppear];
    }];
}

- (void)tableviewAppear{
    
    
    for (NSDictionary *dic in dataArray) {
        WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
        _acnameLab.text = warEty.accountName;
        _acplaceLab.text = warEty.address;
        _acteleLab.text = [NSString stringWithFormat:@"电话:%@",
                           warEty.phone];
        NSString *fnum = [NSString stringWithFormat:@"%@",warEty.isfriend];
        if ([fnum isEqualToString:@"1"]) {
            _acfriBtn.hidden = YES;
        }
        NSString *anum = [NSString stringWithFormat:@"%@",warEty.isApply];
        if ([anum isEqualToString:@"1"]) {
            _applyBtn.hidden = YES;
        }
        NSString *nowid = [NSString stringWithFormat:@"%@",
                           [UserModel sharedInstance].currentUser.ID];
        NSString *sendid = [NSString stringWithFormat:@"%@",
                            warEty.accountId];
        if ([nowid isEqualToString:sendid]) {
            _acfriBtn.hidden = YES;
            _applyBtn.hidden = YES;
        }
        _wplaceLab.text = warEty.sportsHallName;
        _wtypeLab.text = warEty.sportEventName;
        _wmarkLab.text = warEty.remark;
        _nowcouLab.text = [NSString stringWithFormat:@"(%@)",warEty.nowcount];
        _countLab.text = [NSString stringWithFormat:@"%@",warEty.count];
        NSString *num = [NSString stringWithFormat:@"%@",warEty.level];
        _leveLab.text = [Util levelWithstr:num];
        NSString *img = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                         warEty.picPath];
        NSURL *url = [NSURL URLWithString:img];
        [_photoImg setImageWithURL:url];
       _photoImg.contentMode = UIViewContentModeRedraw;
        NSString *headimg = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                         warEty.headPic];
        NSURL *headurl = [NSURL URLWithString:headimg];
        [_headImg setImageWithURL:headurl];

        [self.tableView reloadData];
    }
    
}

- (IBAction)addFriendBtn:(id)sender{
    
    NSString *myid = [UserModel sharedInstance].currentUser.ID;
    NSString *othid = [[dataArray objectAtIndex:0]
                       objectForKey:@"accountId"];
    [[UserModel sharedInstance] addFriendWithmeID:myid otherID:othid returnData:^(BOOL successed, unsigned int value) {
        if (successed) {
            _acfriBtn.hidden = YES;
        }
        
    }];
    
}
- (IBAction)applyBtn:(id)sender{
    NSString *myid = [UserModel sharedInstance].currentUser.ID;
    NSString *warid = [[dataArray objectAtIndex:0]
                       objectForKey:@"id"];
    [[UserModel sharedInstance] applyWarWithMeid:myid warID:warid appData:^(BOOL successed, unsigned int value) {
        if (successed) {
            _applyBtn.hidden = YES;
        }
    }];
}

- (IBAction)seemoreBtn:(id)sender{
    if (cellArray.count == 0) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.labelText = @"没有数据";
        [HUD hide:YES afterDelay:1.5f];
    }
    else{
        UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                       bundle:nil];
        MoreWarMViewC *morevc = [main instantiateViewControllerWithIdentifier:@"MoreWarMViewC"];
        NSString *warid = [[dataArray objectAtIndex:0]
                           objectForKey:@"id"];
        morevc.warID = warid;
        [self.navigationController pushViewController:morevc
                                             animated:YES];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return cellArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WarDetaiCCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"warDetailID"
                                  forIndexPath:indexPath];
    for (NSDictionary *dic in cellArray) {
        WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
        cell.nameLab.text = warEty.wnick;
        NSString *headimg = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                             warEty.wheadpic];
        NSURL *headurl = [NSURL URLWithString:headimg];
        [cell.wheadImg setImageWithURL:headurl];
    
    }
    return cell;
}


@end
